package com.example.securingserver;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author caiwl
 * @date 2020/5/12 14:23
 */
@Component
public class UserDAO {
    @Value("${spring.security.user.name}")
    private String username;
    @Value("${spring.security.user.password}")
    private String password;
    @Value("${spring.security.user.roles}")
    private String[] authorities;

    public UserDetails findByUsernameAndPassword(String username, String password) {
        if (Objects.equals(this.username, username) && Objects.equals(this.password, password)) {
            return User.withUsername(username).password(password).authorities(authorities).build();
        } else {
            return null;
        }
    }
}
